<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <title>作业管理</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.6.8/css/layui.css">
    <script src="https://www.layuicdn.com/layui-v2.6.8/layui.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
</head>
<body>
    <div class="layui-layout layui-layout-admin">
        <!-- 顶部导航栏 -->
        <div class="layui-header">
            <div class="layui-logo layui-hide-xs">作业管理系统</div>
            <ul class="layui-nav layui-layout-left">
                <li class="layui-nav-item layui-this"><a href="javascript:;">作业管理</a></li>
            </ul>
        </div>

        <!-- 主体内容 -->
        <div class="layui-body" style="padding: 15px;">
            <!-- 发布作业表单 -->
            <div class="layui-card">
                <div class="layui-card-header">
                    <span class="layui-font-16">发布新作业</span>
                </div>
                <div class="layui-card-body">
                    <form class="layui-form" action="${pageContext.request.contextPath}/teacher/assignment/publish" method="post">
                        <div class="layui-form-item">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-block">
                                <input type="text" name="title" required lay-verify="required" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">内容</label>
                            <div class="layui-input-block">
                                <textarea name="content" required lay-verify="required" class="layui-textarea" rows="4"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">截止日期</label>
                            <div class="layui-input-block">
                                <input type="text" name="deadline" id="deadline" required lay-verify="required" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit>发布作业</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <!-- 作业列表 -->
            <div class="layui-card" style="margin-top: 20px;">
                <div class="layui-card-header">
                    <span class="layui-font-16">作业列表</span>
                </div>
                <div class="layui-card-body">
                    <table class="layui-table">
                        <thead>
                            <tr>
                                <th>标题</th>
                                <th>发布时间</th>
                                <th>截止日期</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <c:forEach items="${assignments}" var="assignment">
                                <tr>
                                    <td>${assignment.title}</td>
                                    <td><fmt:formatDate value="${assignment.publishTime}" pattern="yyyy-MM-dd HH:mm"/></td>
                                    <td><fmt:formatDate value="${assignment.deadline}" pattern="yyyy-MM-dd HH:mm"/></td>
                                    <td>
                                        <span class="layui-badge ${assignment.status == '进行中' ? 'layui-bg-green' : 'layui-bg-gray'}">
                                            ${assignment.status}
                                        </span>
                                    </td>
                                    <td>
                                        <div class="layui-btn-group">
                                            <button class="layui-btn layui-btn-sm" onclick="viewSubmissions(${assignment.aid})">
                                                <i class="layui-icon layui-icon-form"></i>查看提交
                                            </button>
                                            <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="viewStats(${assignment.aid})">
                                                <i class="layui-icon layui-icon-chart"></i>成绩统计
                                            </button>
                                            <button class="layui-btn layui-btn-sm layui-btn-warm" onclick="editAssignment(${assignment.aid})">
                                                <i class="layui-icon layui-icon-edit"></i>编辑
                                            </button>
                                            <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="deleteAssignment(${assignment.aid})">
                                                <i class="layui-icon layui-icon-delete"></i>删除
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                            </c:forEach>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <script>
        layui.use(['layer', 'form', 'laydate'], function(){
            var layer = layui.layer;
            var form = layui.form;
            var laydate = layui.laydate;
            
            // 初始化日期时间选择器
            laydate.render({
                elem: '#deadline',
                type: 'datetime',
                format: 'yyyy-MM-dd HH:mm:ss', // 指定日期格式
                trigger: 'click'
            });
        });
        
        function viewSubmissions(aid) {
            window.location.href = '${pageContext.request.contextPath}/teacher/assignment/submissions?aid=' + aid;
        }
        
        function viewStats(aid) {
            window.location.href = '${pageContext.request.contextPath}/teacher/assignment/stats?aid=' + aid;
        }
        
        function editAssignment(aid) {
            window.location.href = '${pageContext.request.contextPath}/teacher/assignment/edit?aid=' + aid;
        }
        
        function deleteAssignment(aid) {
            layer.confirm('确定要删除这个作业吗？', function(index){
                var form = $('<form method="post" action="${pageContext.request.contextPath}/teacher/assignment/delete">' +
                           '<input type="hidden" name="aid" value="' + aid + '"></form>');
                $('body').append(form);
                form.submit();
            });
        }
    </script>
</body>
</html> 